#coding=utf-8
'''
# @Name:__init__.py
# @author:mysouil
# @Description:
# @DATE:2022/12/18 15:24
# @Modified By:
'''

from flask import Flask, render_template
from flask_migrate import Migrate
from comment.models import db
from comment.utils.request_hook import request_auth
from manager.resource.student import user_bp, login_view
from settings.default import map_config


def create_app(run_type):
    '''
    创建APP的函数。参数：开发环境、生产环境。
    '''
    # 创建app
    app = Flask(__name__)
    # 加载整个项目的配置
    app.config.from_object(map_config[run_type])

    # 初始化SQLAlchemy
    db.init_app(app)

    # 初始化Migrate
    Migrate(app, db)

    # 注册蓝图
    app.register_blueprint(user_bp)

    # 添加路由
    app.add_url_rule('/','',login_view,methods=['GET'])

    @app.errorhandler(404)
    def error_template(error):
        return render_template('404.html')

    # 添加请求钩子(全局)
    app.before_request(request_auth)  # 所有请求都生效
    
    return app